<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\Entity\User;

class HomeController extends AbstractActionController
{
    protected $_objectManager;
    
    public function contactUsAction()
    { 
        
        
        return new ViewModel();
    }
    
    public function signInAction()
    {
        if ($this->request->isPost()) 
        {
            
            $password = $this->getRequest()->getPost('password');
            $email = $this->getRequest()->getPost('email');
             
            if(isset($password) && isset($email) )
            {
                if(!empty($email) && !empty($password))
                {
                    $users = $this->getObjectManager()->getRepository('\Application\Model\Entity\User')->findBy(array('email' => $email, 'password' => $password));
                   
                    
                    
                    if(!empty($users))
                    {     
                        $user = $users[0];
                        if($user->getEmail() == 'admin@gmail.com' && $user->getPassword() == 'admin')
                        {
                             return $this->redirect()->toRoute('user/wildcard', array('controller' => 'home', 'action' => 'show'));
                        }
                        
                        return $this->redirect()->toRoute('application/wildcard', array('id' => $user->getId()));             
                    }
                    else 
                    {
                       return new ViewModel(array('resp' => 'Niprawidłowy login lub hasło'));                         
                    }                    
                }
                else
                {
                    return new ViewModel(array('resp' => 'Niprawidłowy login lub hasło'));  
                }
            }
        }
        
        return new ViewModel();                
    }
    
    public function signUpAction()
    { 
        if ($this->request->isPost()) 
        {
            
            $user = new User();
            $email = $this->getRequest()->getPost('email');
            $password = $this->getRequest()->getPost('password');
            $confpassword = $this->getRequest()->getPost('password_confirmation');
            
            if(isset($password) && isset($confpassword) && isset($email))
            {
                if($password  == $confpassword)
                { 
                    $user->setEmail($email);
                    $user->setPassword($password);
                    
                    $this->getObjectManager()->persist($user);
                    $this->getObjectManager()->flush();
                    $id = $user->getId();
                    return $this->redirect()->toRoute('home', array('id' => $id));
                }
                else
                {                      
                    return new ViewModel(array(
                        'password' => $password, 
                        'email'    => $email,
                        'resp'     => 'Hasło nie jest poprawne',
                            ));
                }                
            }
        }
        
        return new ViewModel();
    }
    
    public function faqAction()
    { 
        
        return new ViewModel();
    }
    
    protected function getObjectManager()
    {
                
        if (!$this->_objectManager) {
            $this->_objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        }

        return $this->_objectManager;
    }
}
